---
title: "Disease and Conflict Code"
author: "Christina Walker"
date: "3/14/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
rm(list=ls()) # clear environment 
setwd('')
```

```{r}
library(lfe)
library(tidyverse)
library(broom)
library(dplyr)
library(stargazer)
library("Hmisc")

```


```{r}
measles <- read.csv('../data/measles_data.csv', header=FALSE)
measles <- measles[-c(1), ] # remove row
names(measles) <- measles[1,]
measles <- measles[-1,]
scad <- read.csv("../data/scad.csv")

```


SCAD data:

```{r}
scad_sub <- scad[scad$countryname=="Chad"|scad$countryname=="Cameroon"|scad$countryname=="Central African Republic"| scad$countryname=="Republic of Congo",] #subset countries

scad_sub$countryname[scad_sub$countryname == "Republic of Congo"] <- "Congo" # rename to match other data
scad_sub$countryname <- gsub("Republic of Congo", "Congo", scad_sub$countryname) # change country name
scad_sub <- subset(scad_sub,styr >= 1996 & styr <= 2005) # subset years

scad_sub <- rename(scad_sub,Country = countryname) #change variable names
scad_sub <- rename(scad_sub,Year = styr)
scad_sub <- rename(scad_sub,Type = etype)

# create indicator column for unrest
scad_sub$conflict <- 1

scad_sub <- scad_sub %>% group_by(Country, Year) %>%
  summarise(total_conflict = sum(conflict),
            .groups ='drop') #add social unrest for additive index

#add countries years with no conflict
# Chad
chad <- data.frame("Chad","1996",0)
names(chad) <-c("Country","Year","total_conflict")
scad_sub <- rbind(scad_sub, chad)

#Congo
congo <- data.frame("Congo", "1999",0)
names(congo) <-c("Country","Year","total_conflict")
scad_sub <- rbind(scad_sub, congo)

congo2 <- data.frame("Congo", "2001",0)
names(congo2) <-c("Country","Year","total_conflict")
scad_sub <- rbind(scad_sub, congo2)

# group by country
scad_sub <- scad_sub %>%
  group_by(Country) %>%
  mutate(rate_of_change = (total_conflict - lag(total_conflict))) # calculates the rate of change in the total_conflict column by subtracting the previous value from the current value. The result is stored in a new column named rate_of_change


```

Reshape from wide to long panel data:

```{r}
measles <- measles[,order(ncol(measles):1)] # reverse columns
measles <- measles %>%
  select(Country, everything()) # make last column first column

measles <- reshape(measles, 
                   idvar="Country",
                   varying =list(names(measles)[2:48]),
                   timevar = "Year",
                   times = 1974:2020,
                   v.names = "measles_cases",
                   direction = "long") # shift panel data from wide to long 


```

```{r}
# subset years in measles data
measles_sub <- measles[measles$Country=="Chad" |measles$Country=="Central African Republic"| measles$Country=="Congo"| measles$Country=="Cameroon",] # create subset for countries

measles_sub <- measles_sub[measles_sub$Year=="1996" | measles_sub$Year=="1997" |measles_sub$Year=="1998" |measles_sub$Year=="1999"|measles_sub$Year=="2000"|measles_sub$Year=="2001"|measles_sub$Year=="2002"|measles_sub$Year=="2003"|measles_sub$Year=="2004"|measles_sub$Year=="2005",]

```


Merge datasets together:

```{r}
merge <- merge(measles_sub, scad_sub, by = c("Country", "Year")) #merge

merge$time = ifelse(merge$Year >= 2001, 1, 0) #if before or after treatment

merge$treated = ifelse(merge$Country == "Chad" | merge$Country=="Cameroon", 1, 0) # if treated
as.integer(merge$treated)

merge$Group = ifelse(merge$treated==1,"Treated","Control")

merge$did = merge$time * merge$treated #interaction


```


```{r}
plot1 <- subset(merge,Year >= 1999 & Year <= 2003)
ggplot(plot1, aes(x=Year,y=total_conflict, color=Group)) +
  scale_color_manual(values=c('forestgreen','brown4'))+
    stat_summary(geom = 'line') +
    labs(y = "Conflict") +
    geom_vline(xintercept = 2001) +
    theme(panel.grid.major = element_blank(), legend.position=c(.8,.9),panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))

```

DiD for outcome conflict, treatment = measles:

```{r}
merge$time = ifelse(merge$Year >= 2001, 1, 0)
merge$t_ind <- ifelse(merge$Country %in% c("Chad", "Cameroon"), 1, 0)
merge$treated = ifelse(merge$t_ind == "1", 1, 0)
merge$did = merge$time * merge$treated

labels <- c("Treatment Group", "Time","Interaction (DiD)","Constant")

# normal DiD without a lag
didreg = lm(total_conflict ~ treated + time + did, data = merge)
summary(didreg)

# create the 1 year lag
merge <- merge %>%
  group_by(Country) %>%
  mutate(conflict_lagged = lag(total_conflict, order_by = Year))

didlag = lm(conflict_lagged ~ treated + time + did, data = merge)
summary(didlag)

stargazer(
  didlag,
  type = "html", out="tab.html", 
  title = "Measles and Civil Unrest",
  column.labels = c("Civil Unrest","Measles"), 
  dep.var.labels.include = FALSE,
  covariate.labels = labels,
  style = "apsr", # put it in the style of APSR
  model.numbers = FALSE)
  
```


